package com.proxyadvertise.view;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.io.IOException;
import java.util.List;
import java.util.Random;

import javax.swing.JPanel;

import com.proxyadvertise.parse.ConfigFile;

public class IEFrame extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9219667161140016623L;

	private String url;

	private Dimension dim; // 存储屏幕尺寸

	private double width;
	private double height;

	private Robot robot;// 自动化对象

	public IEFrame(String url) {
		this.url = url;

		dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.width = dim.getWidth();
		this.height = dim.getHeight();

		try {
			robot = new Robot();
		} catch (AWTException e) {
			e.printStackTrace();
		}

	}

	public void open() {

		try {
			Runtime.getRuntime().exec("cmd /c  start " + this.url);
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 打开ie后，暂停一小段时间
		try {
			Thread.sleep(Integer.parseInt(ConfigFile
					.getConfigValue("delayTime")));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		// 点击状态栏，移动到可以点击广告链接区域
		String[] statusColumn = ConfigFile.getConfigValue("statusColumn")
				.split(",");
		robot.mouseMove(Integer.parseInt(statusColumn[0]),
				Integer.parseInt(statusColumn[1])); // 移动
		int clickCount = Integer.parseInt(ConfigFile
				.getConfigValue("clickCount"));

		for (int i = 0; i < clickCount; i++) {
			robot.delay(1000);
			robot.mousePress(InputEvent.BUTTON1_MASK); // 按下鼠标左键
			robot.mouseRelease(InputEvent.BUTTON1_MASK); // 释放鼠标左键
		}

		// 暂停
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		// 获取坐标，移动到广告链接
		List<String> coordinateList = ConfigFile.getAllCoordinate();
		int size = coordinateList.size();

		// 随机获取一个坐标信息
		String[] coordinates = coordinateList.get(getRandomNum(size))
				.split(",");
		System.out.println("移动坐标：" + coordinates[0] + "======="
				+ coordinates[1]);
		robot.mouseMove(Integer.parseInt(coordinates[0]),
				Integer.parseInt(coordinates[1])); // 移动
		robot.delay(1000);
		robot.mousePress(InputEvent.BUTTON1_MASK); // 按下鼠标左键
		robot.mouseRelease(InputEvent.BUTTON1_MASK); // 释放鼠标左键

	}

	public void close() {
		
		Point mousepoint = MouseInfo.getPointerInfo().getLocation();
		System.out.println("移动前坐标：" + mousepoint.x + " " + mousepoint.y);

		robot.delay(200);
		robot.mouseMove((int) this.width-10, 8 );
		robot.delay(1000);
		robot.mousePress(InputEvent.BUTTON1_MASK); // 按下鼠标右键
		robot.mouseRelease(InputEvent.BUTTON1_MASK); // 释放鼠标右键
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// String defaultIETaskName = ConfigFile.getConfigValue(
		// "defaultIETaskName" );
		// try {
		// Runtime.getRuntime().exec( "cmd /c  taskkill /f /im " +
		// defaultIETaskName );
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

	}

	/**
	 * 获取随机数
	 * 
	 * @param length
	 * @return
	 */
	private int getRandomNum(int length) {
		Random random = new Random();

		return random.nextInt(length);

	}

	public static void main(String[] args) throws IOException {

		IEFrame ie = new IEFrame("http://tech.sina.com.cn/");

		// ie.open();
		// ie.close();

		int i = 0;
		while (i < 20) {
			System.out.println(i + "===" + ie.getRandomNum(1));
			i++;
		}

	}
}
